<?php

class MessagesController extends \Phalcon\Mvc\Controller
{

    public function indexAction()
    {
		$this->view->setMainView('index');
		$this->view->setVar('page_name', 'Messages');
		$this->view->setVar('content', 'messages/index');
		$this->view->setVar('title', 'Messages');
    }

    public function listAction()
    {
        $recipient_id = $_GET['recipient_id'];
        $sender_id =  $this->session->get('user_id');
        $result = array();

        $data  = ConversationTb::findFirst(array(
                'conditions'=>'(user_id_1 = ?0 AND user_id_2 = ?1) OR (user_id_1 = ?1 AND user_id_2 =?0)',
                'bind'=>array(0=>$sender_id,1=>$recipient_id)
            ));

        if(!$data){
            $result['status'] = false;
            echo json_encode($result);
            $this->view->disable();
            exit();
        }

        $messages = ConversationReply::listMessages($data->id);


        if(!sizeof($messages)){
            $result['status'] = false;
            $result['c_id'] = $data->id;
            echo json_encode($result);
            $this->view->disable();
            exit();   
        }

        $result['status'] = true;
        //echo json_encode($result);
            
        $lists = array();


        foreach ($messages as $value) {
            foreach (get_object_vars($value) as $key=>$columns) {
                $list[$key] = $columns;    
            }
            $lists[] = $list;
        }
        $result['data'] = $lists;
        echo json_encode($result);
        $this->view->disable();
    
    }


    public function sendAction()
    {
    	if(!$this->request->isPost()) die('Not a post');
    	
    	$sender_id = $this->session->get('user_id');
    	$recipient_id = $this->request->getPost('recipient_id');
    	$message = nl2br(htmlentities($this->request->getPost('message')));
    	$result = array();

    	$conv = ConversationTb::findFirst(array(
    			"conditions"=>"(user_id_1 = ?0 AND user_id_2 = ?1) OR (user_id_1 = ?1 AND user_id_2 = ?0)",
    			"bind"=>array(0=>$sender_id,1=>$recipient_id)
    		));

    	$data = array(
    		"sender_id" => $sender_id,
    		"sender_fullname" => UserAccountTb::getFullname($sender_id),
    		"sender_prof_pic" => UserAccountTb::getProfilePicUrl($sender_id),
    		"message" => $message
    	);


    	if(!$conv)
    	{
    		$conv = new ConversationTb();
    		$conv->user_id_1 = $sender_id;
    		$conv->user_id_2 = $recipient_id;
    		if(!$conv->save())
    		{
	    		$result['status'] = false;
	    		$result['data'] = "Message not sent";
	    		echo json_encode($result);
	    		$this->view->disable();
	    		exit();
    		}
    	}
		$reply = new ConversationReply();
		$reply->c_id = $conv->id;
		if(!$reply->save($data))
		{
			$result['status'] = false;
    		$result['data'] = "Message not sent";
    		echo json_encode($result);
    		$this->view->disable();
    		exit();	
		}

        $data['date_created'] = $reply->date_created;
	    $pusher = new Pusher(PUSHER_APP_KEY, PUSHER_APP_SECRET, PUSHER_APP_ID);
        $pusher->trigger('my-channel', 'message-sent', $data,$_POST['socket_id']);
		$result['data'] = $data;
		$result['status'] = true;
    	echo json_encode($result);
    	$this->view->disable();

    }
   
}

